﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class Admin_AdminMasterPage : System.Web.UI.MasterPage
{
    MaintenanceBLL maintenanceLogic = new MaintenanceBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["AccountType"].ToString() != "Admin")
            {
                Response.Redirect("Default.aspx");
                return;
            }
        }
        catch
        {
            Response.Redirect("Default.aspx");
            return;
        }

        if (!Page.IsPostBack)
        {
            Maintenance.MaintenanceRow maintenance = maintenanceLogic.GetMaintenance("Application");
            if (maintenance.IsEnabled)
            {
                lblApplicationMaintenanceMessage.Text = maintenance.Message;
                PanelApplicationMaintenance.Visible = true;
            }
            else
            {
                PanelApplicationMaintenance.Visible = false;
            }
        }

        CalculateConcurrentUsers();
    }
    protected void LoginStatus2_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        try
        {
            int StatsLoginID = Convert.ToInt32(Session["StatsLoginID"].ToString());

            StatisticsBLL statsLogic = new StatisticsBLL();

            Statistics.Stats_LoginRow statsLogin = statsLogic.GetStatsLoginByStatsLoginID(StatsLoginID);
            statsLogic.UpdateLogoutDateTime(DateTime.Now, StatsLoginID);
        }
        finally
        {
            Session.Clear();
            Session.Abandon();
            FormsAuthentication.SignOut();
        }
    }

    private void CalculateConcurrentUsers()
    {
        StatisticsBLL statsLogic = new StatisticsBLL();
        Statistics.Stats_LoginDataTable logins = statsLogic.GetStatsLoginsByDate(DateTime.Now.AddDays(-2), DateTime.Now);

        int counter = 0;
        lblConcurrentUsersCounter.ToolTip = String.Empty;
        foreach (Statistics.Stats_LoginRow login in logins)
        {
            if (!login.IsLogoutDateTimeNull())
            {
                if (login.LogoutDateTime >= DateTime.Now.AddSeconds(-30))
                {
                    counter++;
                    lblConcurrentUsersCounter.ToolTip += login.Username.ToString() + " (" + login.FirstName + " " + login.LastName + ")" + ", ";
                }
            }
        }
        if (lblConcurrentUsersCounter.ToolTip.Length >= 2)
            lblConcurrentUsersCounter.ToolTip = lblConcurrentUsersCounter.ToolTip.Remove(lblConcurrentUsersCounter.ToolTip.Length - 2);

        string strCounter = String.Empty;

		// Change Font Color by the number of users. As number gets higher, the color gets more red.
        if (counter <= 10)
        {
            strCounter = "<span style='color: #006400'>" + counter.ToString() + "</span>";
        }
        else if (counter <= 30)
        {
            strCounter = "<span style='color: #FFD700'>" + counter.ToString() + "</span>";
        }
        else if (counter <= 60)
        {
            strCounter = "<span style='color: #D2691E'>" + counter.ToString() + "</span>";
        }
        else if (counter <= 100)
        {
            strCounter = "<span style='color: #FA8072'>" + counter.ToString() + "</span>";
        }
        else
        {
            strCounter = "<span style='color: #FF0000'>" + counter.ToString() + "</span>";
        }

        lblConcurrentUsersCounter.Text = strCounter;
    }

    protected void imgbtnRefreshConcurrentUsersCounter_Click(object sender, ImageClickEventArgs e)
    {
        CalculateConcurrentUsers();
    }
}
